﻿#ifndef AIWEBSOCKET_H
#define AIWEBSOCKET_H

/*
* @author   Puuck
* @date     2020-10-20
*/

#include <QObject>
#include <QJsonObject>
#include <iostream>
#include <config/asio_no_tls_client.hpp>
#include <client.hpp>

#include "AiPtuStruct.h"

typedef websocketpp::client<websocketpp::config::asio_client> WsClient;
using websocketpp::lib::placeholders::_1;
using websocketpp::lib::placeholders::_2;
typedef websocketpp::config::asio_client::message_type::ptr message_ptr;

class AiWebsocket : public QObject
{
    Q_OBJECT
public:
    explicit AiWebsocket(QObject* parent = nullptr);

    void conn(const QString& ws);
    void disconn();
    void sendRequest(const QString& method, int seq, QJsonObject body);

    void sendBinaryMessage(const QByteArray& message);

    bool connected() const;
    void setConnected(bool connected);

    void terminate();

    void setChn(int chn);

private:
    void onTextMessageReceived(const QString& message);
    void onBinaryMessageReceived(const QByteArray& message);

    void handleDeviceInfo(QJsonObject obj);
    void handleDeviceRunInfo(QJsonObject obj);
    void handleNetworkParam(QJsonObject obj);

    void handleAlgoParam (QJsonObject obj);

    AlarmExtern handleAlarmExtern(QJsonObject obj);
    void handleDetPeopleParam(QJsonObject obj);

    void handleCameraInfo(QJsonObject obj);
    void handleCameraParam(QJsonObject obj);

    void handleDetBeltParam(QJsonObject obj);

    void handleAppUpgrade(QJsonObject obj);

    void handleCoalSpdParam(QJsonObject obj);
    void handleCoalSpdValue(QJsonObject obj);

    void handleOfflineSnapParam(QJsonObject obj);

    void init();
    void on_open(websocketpp::connection_hdl hdl);
    void on_fail(websocketpp::connection_hdl hdl);
    void on_close(websocketpp::connection_hdl hdl);
    void on_message(websocketpp::connection_hdl hdl, message_ptr msg);

private:
    bool m_connected;

    enum WSBIN_TYPE
    {
        WSBIN_TYPE_NULL = 0,
        WSBIN_TYPE_IMAGE,          // 实时图片、报警图片
        WSBIN_TYPE_FILE,           // 文件
        WSBIN_TYPE_SCREENSHOTS,    // 截图
        WSBIN_TYPE_VOICE,          // 音频文件
    };

    WsClient m_wsClient;
    websocketpp::lib::shared_ptr<websocketpp::lib::thread> m_wsThread;
    websocketpp::connection_hdl m_hdl;
    std::string m_uri;

    int m_chn;

signals:
    void sigConnectState(bool state);
    void sigConnectFail();
    void sigReconnect();

    void sigDeviceInfo(DeviceInfo deviceInfo);
    void sigDeviceRunInfo(DeviceRunInfo deviceRunInfo);
    void sigNetworkParam(NetworkParam networkParam);
    void sigSetNetworkParamSuccess();

    void sigSyncDateTimeSuccess();

    void sigAlgoParam(AlgoSet);

    void sigAlarmImage(WsBinHeader header, QByteArray image, AlarmExtern alarmExtern);
    void sigDetPeopleParam(DetPeopleParam detPeopleParam);

    void sigRealtimeImage(WsBinHeader header, QByteArray image);

    void sigCameraInfo(CameraInfo cameraInfo);
    void sigCameraParam(CameraParam cameraParam);
    void sigSetCameraParamSuccess();

    void sigDetBeltParam(DetBeltParam detBeltParam);

    void sigAppUpgrade(AppUpgrade appUpgrade);

    void sigCoalSpdParam(CoalSpdParam coalSpdParam);
    void sigCoalSpdValue(CoalSpdValue coalSpdValue);

    void sigOfflineSnapParam(OfflineSnapParam offlineSnapParam);

    void sigVoiceTalkOut(WsBinHeader header, QByteArray voice);

    void sigGetDeviceInfo(QJsonObject);
    void sigGetDeviceRunInfo(QJsonObject);
    void sigGetNetworkParam(QJsonObject);

    void sigGetAlgoParam(QJsonObject);
    void sigGetExternalParam(QJsonObject);
    void sigGetAlarmOut(QJsonObject);
    void sigGetZoneParam(QJsonObject);
    void sigGetAlgoInfo(QJsonObject);

    void  sigGetAlgoPeopleCountParam(QJsonObject);

};

#endif    // AIWEBSOCKET_H
